package com.fp.m7.client.place;

import net.customware.gwt.presenter.client.gin.ProvidedPresenterPlace;
import net.customware.gwt.presenter.client.place.PlaceRequest;

import com.fp.m7.client.common.ProfileTab;
import com.fp.m7.client.common.TimeUnit;
import com.fp.m7.client.presenter.ProfilePresenter;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * Profile place - both authorized and pubic access. Providing a nick means that one wants to see somebody else's
 * profile. Not providing the nick param while being logged in means one wants to see his/her own profile. The data
 * displayed at profile page combines: comments, followers list, following list, and some basic info about the user.
 * 
 * @author 7
 */
public class ProfilePlace extends ProvidedPresenterPlace<ProfilePresenter> {

    public static final String NAME = "profile";
    // params:
    public static final String NICKNAME = "nick";
    public static final String PAGE_NO = "page";
    public static final String TIME_UNIT = "unit";
    public static final String TAB = "tab";

    private String pageNo;
    private String timeUnit;
    private String tab;
    private String nick;

    @Inject
    public ProfilePlace(Provider<ProfilePresenter> presenter) {
        super(presenter);
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    protected void preparePresenter(PlaceRequest request, ProfilePresenter presenter) {
        presenter.setNickname(nick = request.getParameter(NICKNAME, null));

        pageNo = request.getParameter(PAGE_NO, null);
        if (pageNo != null) {
            try {
                Integer pageNoParsed = Integer.valueOf(pageNo);
                presenter.setPageNo(pageNoParsed);
            } catch (NumberFormatException nfe) {
                // wrong format
                presenter.setPageNo(1);
            }
        }

        timeUnit = request.getParameter(TIME_UNIT, null);
        if (timeUnit != null) {
            try {
                TimeUnit timeUnitParsed = TimeUnit.valueOf(timeUnit);
                presenter.setTimeUnit(timeUnitParsed);
            } catch (IllegalArgumentException iae) {
                // non-existent value
            }
        }

        tab = request.getParameter(TAB, null);
        if (tab != null) {
            try {
                presenter.displayTab(ProfileTab.valueOf(tab.toUpperCase()));
            } catch (IllegalArgumentException iae) {
                // don't you care
            }
        }
    }

    @Override
    protected PlaceRequest prepareRequest(PlaceRequest request, ProfilePresenter presenter) {
        if (nick != null) {
            request = request.with(NICKNAME, nick);
        }
        if (pageNo != null) {
            request = request.with(PAGE_NO, pageNo);
        }
        if (timeUnit != null) {
            request = request.with(TIME_UNIT, timeUnit);
        }
        if (tab != null) {
            request = request.with(TAB, tab);
        }
        return request;
    }
}
